
package com.jf.cloud.service.sysmanager.remote.api;

import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.dao.*;
import com.jf.cloud.service.sysmanager.entity.*;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反馈控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/appPubgProblem")
public class AppPubgProblemController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(AppPubgProblemController.class);

	@Resource
	private TokenHandler tokenHandler;
	
	@Resource
	private IPubgUserService pubgUserService;

	@Resource
	private IPubgUserTeamService pubgUserTeamService;

	@Resource
	private IPubgUserClubService pubgUserClubService;

	@Resource
	private IPubgProblemService pubgProblemService;

	/**
	 * 意见反馈
	 *
	 * @author 高冬冬 创建时间：2018-04-08
	 */
	@ResponseBody
	@RequestMapping("/addPubgProblem")
	public Result<Map<String, Object>> addPubgProblem(String token, HttpServletRequest request){
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		result.setCode(-1);
		result.setMessage("意见反馈失败！");
		try {
			if (isReCommit(request)) {
				result.setMessage("请勿重复提交！");
				return result;
			}
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.setCode(-2);
				result.setMessage("请重新登陆！");
				return result;
			}
			PubgUser pubgUser = this.pubgUserService.findByPK(Long.valueOf(userId));

			Long feedbackTypeId = getLongValue(request, "feedbackTypeId", 1l);
			String content = getParamValueString(request, "content", "");
			String contactWay = getParamValueString(request, "contactWay", "");
			String imagePath = getParamValueString(request, "imagePath", "");
			if (StringUtil.isEmpty(content)) {
				result.setMessage("描述不能为空！");
				return result;
			}
			if (StringUtil.isEmpty(contactWay) || !StringUtil.isHandset(contactWay)) {
				result.setMessage("联系手机格式错误！");
				return result;
			}
			PubgProblem pubgProblem  = new PubgProblem();
			pubgProblem.setFeedbackTypeId(feedbackTypeId);
			if (feedbackTypeId == 1) {
				pubgProblem.setFeedbackTypeName("问题");
			} else if (feedbackTypeId == 2) {
				pubgProblem.setFeedbackTypeName("建议");
			} else if (feedbackTypeId == 3) {
                pubgProblem.setFeedbackTypeName("服务举报");
            } else if (feedbackTypeId == 4) {
                pubgProblem.setFeedbackTypeName("赛事举报");
            }
			pubgProblem.setUserId(pubgUser.getRowId());
			pubgProblem.setUserAccount(pubgUser.getUserAccount());
			pubgProblem.setContent(content);
			pubgProblem.setContactWay(contactWay);

            if (StringUtil.isNotEmpty(imagePath)) {
                pubgProblem.setImg(imagePath);
            }
			// 当前操作人的 俱乐部关系
			Query query = new Query();
			query.put("USER_ID", userId);
			query.put("RELATION_STATE", 2);
			List<PubgUserClub> clubList = pubgUserClubService.findList(query);
			if (clubList != null && !clubList.isEmpty()) {
				pubgProblem.setClubId(clubList.get(0).getClubId());
				pubgProblem.setClubName(clubList.get(0).getClubName());
			}
			// 当前操作人的 战队关系
			query = new Query();
			query.put("USER_ID", userId);
			query.put("RELATION_STATE", 2);
			List<PubgUserTeam> userTeamList = pubgUserTeamService.findList(query);
			if (userTeamList != null && !userTeamList.isEmpty()) {
				pubgProblem.setTeamId(userTeamList.get(0).getTeamId());
				pubgProblem.setTeamName(userTeamList.get(0).getTeamName());
			}
			pubgProblem.setSubmitTime(DateUtil.getCurrentDateTime());
			pubgProblemService.saveOrUpdatePubgProblem(pubgProblem);
            Map<String, Object> dataTemp = new HashMap<String, Object>();
            dataTemp.put("rowId", pubgProblem.getRowId());
            result.setData(dataTemp);
			result.setCode(0);
			result.setMessage("意见反馈成功！");
		} catch (Exception e) {
			log.error("意见反馈出错！", e);
		}
		return result;
	}
}
